<?php
/**
 * 基础操作——增
 */

class IncreaseCTR extends Controller
{
    private $config;
    function init()
    {
        $ConfigModel = new Model('config');
        $config = $ConfigModel->fetch();
        $this->config = $config;
        $this->assign('config', $config);
    }

    function post()
    {
        //dump($this->isPost());
        if ($this->isPost() && isset($_GET['id']) && !empty($_GET['id'])) {
            $title = $_POST['title'];
            $content = $_POST['content'];
            $user = Session::get('user');
            if ($user) {
                $sid = $_GET['id'] + 0;
                $TopicModel = new Model('topic');
                if ($TopicModel->insert(array(
                    'sid' => $sid,
                    'uid' => $user['id'],
                    'title' => $title,
                    'content' => $content,
                    'ptime' => time()))) {
                    $insertid = $TopicModel->getInsertID();
                    $this->updateSectionNum($sid);
                    $this->success('发表成功', $this->urlCreate('view', 'topic', array('id' => $insertid)));
                } else {
                    $this->error('系统出错，请稍后再试', 'javascript:history.go(-1)');
                }
            } else {
                $this->error('您还没有登录，请登录后再发表帖子', $this->urlCreate('user', 'login'));
            }
        } else {
            $this->error('非法访问', $this->urlCreate('index', 'index'));
        }
    }

    function reply()
    {
        if ($this->isPost() && isset($_GET['id']) && !empty($_GET['id'])) {
            $id = $_GET['id'] + 0;
            $content = $_POST['content'];
            $user = Session::get('user');
            if ($user) {
                $TopicModel = new Model('topic');
                $topic = $TopicModel->fetch('*', array('id' => $id));
                $ReplyModel = new Model('reply');
                if ($ReplyModel->insert(array(
                    'tid' => $id,
                    'uid' => $user['id'],
                    'rid' => $topic['uid'],
                    'content' => $content,
                    'ptime' => time()))) {
                    $this->success('回复成功', $this->urlCreate('view', 'topic', array('id' => $id)));
                    exit;
                } else {
                    $this->error('系统出错', $this->urlCreate('index', 'index'));
                }
            } else {
                $this->error('您还没有登录，请登录后再发表帖子', $this->urlCreate('user', 'login'));
            }
        } else {
            $this->error('非法访问', $this->urlCreate('index', 'index'));
        }
    }

    protected function getUserByID($id)
    {
        $UserModel = new Model('user');
        $user = $UserModel->fetch('*', array('id' => $id));
        if ($user) {
            return $user['user'];
        } else {
            return '黑户';
        }
    }

    private function updateSectionNum($sid)
    {
        $SectionModel = new Model('section');
        $section = $SectionModel->fetch('*', array('id' => $sid));
        $lastday = empty($section['cache_time']) ? '0' : $section['cache_time']; //缓存的上次发表话题的日期
        if ($lastday + 86400 < time() + 1) {
            //如果上次发表话题日的最后一秒小于当前时间，说明这是新的一天
            $today = strtotime(date('Y-m-d', time())); //今天的日期
            $section = $SectionModel->fetch('*', array('id' => $sid));
            $newtotal = $section['total'] + 1;
            $SectionModel->update(array(
                'today' => 1,
                'total' => $newtotal,
                'cache_time' => $today), array('id' => $sid));
        } else {
            //当天
            $section = $SectionModel->fetch('*', array('id' => $sid));
            $newtotal = $section['total'] + 1;
            $SectionModel->update(array('today' => $section['today'] + 1, 'total' => $newtotal),
                array('id' => $sid));
        }
    }

    function upload()
    {
        if (isset($_GET['id']) && !empty($_GET['id'])) {
            $id = $_GET['id'];
        } else {
            $this->error('非法访问！', $this->urlCreate('index', 'index'));
            exit;
        }
        $TopicModel = new Model('topic');
        $topic = $TopicModel->fetch('*', array('id' => $id));
        $login_user = Session::get('user');
        if ($topic['uid'] == $login_user['id']) {
            if ($this->isPost()) {
                $upload = Upload::getInstance();
                $upload->setPath(date('Ymd', time()));
                $upload->setRootPath(APP . '/upload');
                if (!empty($this->config['upload_prefix'])) {
                    $pre = explode(';', $this->config['upload_prefix']);
                    $upload->setPrefix($pre);
                }
                if ($res = $upload->run('upload')) {
                    $UploadModel = new Model('upload');
                    if ($UploadModel->insert(array(
                        'uid' => $topic['uid'],
                        'name' => $res['name'],
                        'path' => $res['path'],
                        'type' => 'topic',
                        'tid' => $topic['id']))) {
                        $this->success('上传成功', $this->urlCreate('view', 'topic', array('id' => $id)));
                        exit;
                    } else {
                        unlink($res['path']);
                        $this->error('上传失败', 'javascript:history.go(-1);');
                        exit;
                    }
                } else {
                    $res = $upload->getError();
                    $this->error($res, 'javascript:history.go(-1);');
                }
            } else {
                $this->assign('topic', $topic);
                $this->assign('title', '上传附件 - ' . $this->config['name']);
                $this->display();
            }
        } else {
            $this->error('非法访问！', $this->urlCreate('index', 'index'));
        }
    }
}

?>